﻿using System;
using System.Windows.Data;
using ISRecord.View.Services;

namespace ISRecord.View
{
    public class DateConverter : IValueConverter
    {

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            
            string s = (string)value;
            if (s == "")
            {
                return value;
            }
            return Helper.PutMask(s);
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string enteredDate = (string)value;
            string resultDate;
            //if (enteredDate == "")
            //{
            //    return enteredDate;
            //}
            //string unmaskedDate = Helper.UnmaskDate(enteredDate);
            //if (Helper.IsValidDate(unmaskedDate))
            //{
            //    return unmaskedDate;
            //}
            //else
            //{
            //    throw new ArgumentException("Date is not valid");
            //}
            if (Helper.TryUnmaskDate(enteredDate, out resultDate))
            {
                return resultDate;
            }
            return enteredDate;
            
        }
    }
}
